package com.util;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CheckCode extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public CheckCode() {
        super();
        // TODO Auto-generated constructor stub
    }
//    生成随机颜色的方法
    public Color getRandColor(int s, int e){
		Random random=new Random();
		if(s>255){
			s=255;
		}
		if(e>255){
			e=255;
		}
		int r=s+random.nextInt(e-s);
		int g=s+random.nextInt(e-s);;
		int b=s+random.nextInt(e-s);;
		return new Color(r,g,b);
	}    
//    生成随机汉字的方法
    public String getWord(){
    	//利用汉字机内码 随机生成汉字
		String word="";
		String[] wBase={"0","1","2","3","4","5","6","7"
						,"8","9","a","b","c","d","e","f"};
		//机内码是由4个16进制数组成的
		//生成第一个机内码：
		Random r=new Random();
		int w1=r.nextInt(3)+11;
		String str_w1=wBase[w1];
		int w2;
		if(w1 == 13){
			w2 = r.nextInt(7);
		}else{
			w2 = r.nextInt(16);
		}
		String str_w2=wBase[w2];
		int w3=r.nextInt(6)+10;
		String str_w3=wBase[w3];
		int w4;
		if(w3==10){
			w4=r.nextInt(15)+1;
		}else if(w3==15){
			w4=r.nextInt(15);
		}else{
			w4=r.nextInt(16);
		}
		String str_w4=wBase[w4];
		System.out.println(word);
		byte[] wBytes = new byte[2];
		String w12 = str_w1 + str_w2;
		int wb1=Integer.parseInt(w12, 16);
		wBytes[0]=(byte)wb1;
		String w34 = str_w3 + str_w4;
		int wb2=Integer.parseInt(w34, 16);
		wBytes[1]=(byte)wb2;
		word=new String(wBytes);
    	return word;
    }
    
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		设置不缓存图片
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "No-cache");
		response.setDateHeader("Expires", 0);
//		指定生成的响应图片
		response.setContentType("image/jpeg");
		int width=86;
		int height=22;
		BufferedImage image=new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
		Graphics g=image.getGraphics();
		Graphics2D g2d=(Graphics2D)g;
		Random random=new Random();
		Font mFont=new Font("黑体", Font.BOLD,16);
		g.setColor(getRandColor(200,250));
//		绘制背景
		g.fillRect(0, 0, width, height);
//		设置字体
		g.setFont(mFont);
		g.setColor(getRandColor(180,200));
//		绘制100根颜色和位置全部为随机产生的线条，该线条为2f。
//		这段for可以不用
		for(int i=0;i<100;i++){
			int x=random.nextInt(width-1);
			int y=random.nextInt(height-1);
			int x1=random.nextInt(6)+1;
			int y1=random.nextInt(12)+1;
			BasicStroke bs=new BasicStroke(2f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
			Line2D line=new Line2D.Double(x,y,x+x1,y+y1);
			g2d.setStroke(bs);
//			绘制直线
			g2d.draw(line);
		}
		
//		输出随机验证文字
		String sRand="";
		String ctmp="";
		int itmp=0;
		for(int i=0;i<4;i++){
			switch (random.nextInt(4)){
			case 1:
//				生成A~Z
				itmp=random.nextInt(26)+65;
				ctmp=String.valueOf((char)itmp);
				break;
			case 2:
//				生成汉字
				ctmp=getWord();
				break;
			default:
//				生成0~9
				itmp=random.nextInt(10)+48;
				ctmp=String.valueOf((char)itmp);
				break;
			}
			sRand+=ctmp;
			Color color=new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110));
			g.setColor(color);
//			将生成的随机数进行随机缩放并旋转指定角度
//			将文字旋转指定角度
			Graphics2D g2d_word=(Graphics2D)g;
			AffineTransform trans=new AffineTransform();
			trans.rotate(random.nextInt(45)*3.14/180,15*i+8,7);
//			缩放文字
			float scaleSize=random.nextFloat()+0.8f;
			if(scaleSize>1f){
				scaleSize=1f;
			}
			trans.scale(scaleSize,scaleSize);
			g2d_word.setTransform(trans);
			g.drawString(ctmp, 15*i+18, 14);
		}
//		将生成的验证码保存到Session中
//		System.out.println(sRand);
		HttpSession session=request.getSession(true);
		session.setAttribute("randCheckCode", sRand);
//		输出生成的验证码图片
		g.dispose();
		ImageIO.write(image, "JPEG", response.getOutputStream());
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

}
